<!-- Modal.vue 弹窗组件 -->
<template>
  <!-- 遮罩层 -->
  <transition name="modal-fade">
    <div
      v-if="visible"
      class="modal-mask"
      @click.self="handleOverlayClick"
    >
      <div class="modal-container">
        <!-- 头部 -->
        <div class="modal-header">
          <h3 class="modal-title">{{ title }}</h3>
          <button
            v-if="showClose"
            class="modal-close-btn"
            @click="handleClose"
          >
            &times;
          </button>
        </div>
        
        <!-- 内容区域 -->
        <div class="modal-content">
          <component
            :is="contentComponent"
            v-bind="componentProps"
            v-on="componentEvents"
            ref="contentComponent"
          />
        </div>

        <!-- 底部按钮（当不需要自定义按钮时显示） -->
        <div
          v-if="!customButton"
          class="modal-footer"
        >
          <button
            class="modal-btn modal-btn--cancel"
            @click="handleCancel"
          >
            取消
          </button>
          <button
            class="modal-btn modal-btn--confirm"
            @click="handleConfirm"
          >
            确定
          </button>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
export default {
  name: 'CommandModal',

  props: {
    // 弹窗标题
    title: {
      type: String,
      default: '提示'
    },
    // 内容组件
    contentComponent: {
      type: [Object, Function],
      required: true
    },
    // 传递给内容组件的props
    componentProps: {
      type: Object,
      default: () => ({})
    },
    // 传递给内容组件的事件
    componentEvents: {
      type: Object,
      default: () => ({})
    },
    // 是否显示关闭按钮
    showClose: {
      type: Boolean,
      default: true
    },
    // 点击遮罩层是否关闭
    closeOnClickOverlay: {
      type: Boolean,
      default: false
    },
    // 是否使用自定义按钮
    customButton: {
      type: Boolean,
      default: false
    }
  },

  data() {
    return {
      visible: false,
      resolvePromise: null,
      rejectPromise: null
    }
  },

  methods: {
    // 打开弹窗
    show() {
      this.visible = true
      return new Promise((resolve, reject) => {
        this.resolvePromise = resolve
        this.rejectPromise = reject
      })
    },

    // 关闭弹窗
    close() {
      this.visible = false
      this.$destroy()
      this.$el.parentNode.removeChild(this.$el)
    },

    // 处理遮罩层点击
    handleOverlayClick() {
      if (this.closeOnClickOverlay) {
        this.handleCancel()
      }
    },

    // 确认操作
    async handleConfirm() {
      try {
        // 获取内容组件的数据
        const data = await this.$refs.contentComponent?.getCallbackData?.()
        this.resolvePromise?.(data)
        this.close()
      } catch (error) {
        console.error('弹窗确认操作出错:', error)
      }
    },

    // 取消操作
    handleCancel() {
      this.rejectPromise?.(new Error('用户取消操作'))
      this.close()
    },

    // 关闭弹窗
    handleClose() {
      this.handleCancel()
    }
  }
}
</script>

<style scoped>
/* 入场动画 */
.modal-fade-enter-active,
.modal-fade-leave-active {
  transition: opacity 0.3s;
}
.modal-fade-enter,
.modal-fade-leave-to {
  opacity: 0;
}

/* 遮罩层样式 */
.modal-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 99;
}

/* 弹窗容器 */
.modal-container {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  min-width: 520px;
  max-width: 80vw;
  overflow: hidden;
}

/* 头部样式 */
.modal-header {
  display: flex;
  align-items: center;
  padding: 16px 24px;
  border-bottom: 1px solid #f0f0f0;
}

.modal-title {
  margin: 0;
  font-size: 16px;
  font-weight: 500;
  color: rgba(0, 0, 0, 0.85);
  flex: 1;
}

.modal-close-btn {
  border: none;
  background: none;
  font-size: 20px;
  color: rgba(0, 0, 0, 0.45);
  cursor: pointer;
  padding: 0 8px;
  transition: color 0.2s;
}

.modal-close-btn:hover {
  color: rgba(0, 0, 0, 0.75);
}

/* 内容区域 */
.modal-content {
  padding: 24px;
  min-height: 100px;
  max-height: 60vh;
  overflow: auto;
}

/* 底部按钮区域 */
.modal-footer {
  padding: 16px 24px;
  border-top: 1px solid #f0f0f0;
  text-align: right;
}

.modal-btn {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.2s;
  margin-left: 12px;
}

.modal-btn--cancel {
  background: #fff;
  border: 1px solid #d9d9d9;
  color: rgba(0, 0, 0, 0.85);
}

.modal-btn--cancel:hover {
  border-color: #409eff;
  color: #409eff;
}

.modal-btn--confirm {
  background: #409eff;
  color: #fff;
}

.modal-btn--confirm:hover {
  background: #66b1ff;
}
</style>
